﻿using Caliburn.Micro;
using myapp.Databases.DAO;
using myapp.Events;
using myapp.Instances;
using myapp.ViewModels;
using myapp.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using myappcore.ViewModels;
using myapp.Events.ViewModel;
using System.Collections.ObjectModel;

namespace myapp.ViewModels
{
    /// <summary>
    /// Function Group List ViewModel
    /// </summary>
    public class FunctionGroupListViewModel : IBaseContentViewModel, IHandle<ViewModelFunctionGroupSelectedEvent>
    {
        #region Attribute

        private IObservableCollection<FunctionGroupViewModel> _functionGroupViewModelList;

        private static FunctionGroupListViewModel _instance;
        #endregion

        #region Properties

        /// <summary>
        /// List of Function Group List
        /// </summary>
        public IObservableCollection<FunctionGroupViewModel> FunctionGroupViewModelList
        {
            get
            {
                return _functionGroupViewModelList;
            }
        }

        /// <summary>
        /// Get Instance of Function Group View Model
        /// </summary>
        public static FunctionGroupListViewModel Instance
        {
            get
            {
                if (null == _instance)
                {
                    _instance = new FunctionGroupListViewModel();
                    _instance.IsChanged = true;
                }
                return _instance;
            }
        }
        #endregion

        #region Constructure
        public FunctionGroupListViewModel()
        {
            MainInstance.Instance.EventAggregator.Subscribe(this);
            DisplayName = "Nhóm Chức Năng";
            CurrentColor = MainInstance.Instance.ColorConfig.FunctionGroupNormal;
            _functionGroupViewModelList = new BindableCollection<FunctionGroupViewModel>();
            foreach (FunctionGroup group in FunctionGroupDao.FunctionGroupList())
            {
                FunctionGroupViewModel functionGroupView = new FunctionGroupViewModel()
                {
                    FunctionGroup = group,
                    CurrentColor = MainInstance.Instance.ColorConfig.FunctionGroupNormal
                };
                functionGroupView.FunctionViewModelList = new BindableCollection<FunctionViewModel>();
                foreach (Function fun in FunctionDao.FunctionListOfGroup(group))
                {
                    FunctionViewModel functionView = new FunctionViewModel()
                    {
                        CurrentColor = MainInstance.Instance.ColorConfig.FunctionNormal,
                        Function = fun,
                        ActionViewModel = MainInstance.Instance.ActionViewModelByCode(typeof(PossitonManagerViewModel).ToString())
                    };
                    functionGroupView.FunctionViewModelList.Add(functionView);
                }
                if (functionGroupView.FunctionViewModelList.Count > 0)
                {
                    _functionGroupViewModelList.Add(functionGroupView);
                }
            }

        }

        #endregion

        #region Method

        /// <summary>
        /// Process handle selected Function group
        /// </summary>
        /// <param name="message"></param>
        public void Handle(ViewModelFunctionGroupSelectedEvent message)
        {
            if (message != null && message.FunctionGroupViewModel != null)
            {
                message.FunctionGroupViewModel.CurrentColor = MainInstance.Instance.ColorConfig.PosstionGroupSelected;

                for (int i = 0; i < _functionGroupViewModelList.Count; i++)
                {
                    if (_functionGroupViewModelList[i].FunctionGroup != message.FunctionGroupViewModel.FunctionGroup)
                    {
                        if (_functionGroupViewModelList[i].CurrentColor == MainInstance.Instance.ColorConfig.FunctionGroupSelected)
                        {
                            _functionGroupViewModelList[i].CurrentColor = MainInstance.Instance.ColorConfig.FunctionGroupNormal;
                            break;
                        }
                    }
                }

            }
        }
        #endregion
    }
}
